<?php

namespace App\Hospital\Controllers;

use App\Admin\Extensions\ChildBigExporter;
use App\Admin\Tools\SignExporterTools;
use App\Hospital\Models\FamilyModel;
use App\Hospital\Repositories\HospitalUserFamily;
use App\Models\Area;
use App\Models\HospitalTeam;
use App\Models\HospitalUserFamily as ModelsHospitalUserFamily;
use App\Models\ImportFamily;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Support\Facades\DB;

class HospitalUserFamilyController extends AdminController
{
    public function title(){

        $type = request('type');
        $status = request('status');
        $dataType = request()->input('data_type');

        if($dataType){
            if($dataType==1){
                return $this->title = "健康人群(儿童)";
            }
            if($dataType==2){
                return $this->title = "健康人群(孕妇)";

            }
        }elseif($type){
            return $this->title = FamilyModel::$typeTxt[$type]."签约";
        }else{
            return parent::title();
        }
    }
    protected function description()
    {
        $status = request('status');
        $dataType = request()->input('data_type');

        if($status==0){
            $this->description['index'] = '此列表为解约的居民，曾经在本社区签约过';
        }
        if($dataType){
            if($dataType==1){
                 $this->description['index'] = "签约居民超过7岁后将转为健康人群";
            }
            if($dataType==2){
                 $this->description['index'] = "签约居民不再是孕妇身份后将转为健康人群";
            }
        }

        return $this->description;
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
//        $model = ModelsHospitalUserFamily::query()->whereHas() 'family',function($query){
//            $query->where('birthday','>',date('Y-m-d',strtotime('-1 day')));
//        });
        $dataType = request()->input('data_type');
        $sql = "SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));";
        DB::statement($sql);
        return Grid::make(new HospitalUserFamily(['user','family','team','import']), function (Grid $grid) use($dataType) {
            $grid->model()->where('hospital_id',Admin::user()->hospital_id);
            if($dataType) {
                $grid->model()->where('status',2)->groupBy('family_id');
            }

            $grid->column('family.name','姓名');
            $grid->column('user.phone','联系电话')->display(function($value){
                return substr_replace($value, '****', 3, 4);
            });
            $grid->column('family.idcard_type','证件类型')->using(FamilyModel::$idCardTypeTxt);
            $grid->column('family.idcard','证件号')->display(function($value){
                return $value?substr_replace($value, '********', 6, 8):'';
            });
            $grid->column('family.birthday','生日');

            $grid->column('family.address','现住址')->display(function($value){

                $area = Area::query()->whereIn('id',[$this->province,$this->city,$this->county])->pluck('title');
                return implode(',',$area->toArray()).$value;
            });
            $grid->column('team.name','签约团队名称');
            $grid->column('status')->using(ModelsHospitalUserFamily::$statusTxt);
            $grid->column('type')->using(ModelsHospitalUserFamily::$typeTxt);
            $grid->column('starttime','本次签约时间')->display(function($value){

                return $this->starttime."至".$this->endtime;
            });
            $grid->column('created_at','首次签约日期')->display(function($value){

                return date('Y-m-d',strtotime($this->created_at));
            });
            Tooltip::make('.icon-help-circle')->title('此标记为该用户是否与同步数据关联成功');

            $grid->column('import','是否管辖<i class="feather icon-help-circle"></i>')->display(function ($value){
                return $value->pluck('type');
            })->map(function($value){

                return ImportFamily::$typeTxt[$value];
            })->badge();



            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand(false);
                $filter->equal('type')->select(\App\Models\HospitalUserFamily::$typeTxt);
                $filter->equal('status')->select(\App\Models\HospitalUserFamily::$statusTxt);
                $filter->equal('import.type','管辖类型')->select(ImportFamily::$typeTxt);
                $filter->like('family.name','签约人姓名');
                $filter->equal('user.phone','联系电话');
                $filter->between('created_at','首次签约时间')->date();
                $filter->between('starttime','本次签约时间')->date();
                $filter->equal('team.id','签约团队')->select(HospitalTeam::query()->where('hospital_id',Admin::user()->hospital_id)->pluck('name','id')->toArray());
            });




            $grid->disableCreateButton();
            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableRowSelector();
            $grid->showColumnSelector();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new HospitalUserFamily(['user','family','team','import']), function (Show $grid) {
            $grid->field('family.name','姓名');
            $grid->field('user.phone','联系电话');
            $grid->field('family.idcard_type','证件类型')->using(FamilyModel::$idCardTypeTxt);
            $grid->field('family.idcard','证件号');
            $grid->field('family.birthday','生日');

            $grid->field('family.address','现住址')->as(function($value){

                $area = Area::query()->whereIn('id',[$this->province,$this->city,$this->county])->pluck('title');
                return implode(',',$area->toArray()).$value;
            });
            $grid->field('team.name','签约团队名称');
            $grid->field('status')->using(ModelsHospitalUserFamily::$statusTxt);
            $grid->field('type')->using(ModelsHospitalUserFamily::$typeTxt);
            $grid->field('starttime','本次签约时间')->as(function($value){

                return $this->starttime."至".$this->endtime;
            });
            $grid->field('created_at','首次签约日期')->as(function($value){

                return date('Y-m-d',strtotime($this->created_at));
            });
            $grid->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });

        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new HospitalUserFamily(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('family_id');
            $form->text('hospital_team_id');
            $form->text('hospital_id');
            $form->text('status');
            $form->text('endtime');
            $form->text('starttime');
            $form->text('type');
            $form->text('is_sign');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
